package tempEngine.playerInput;

import java.awt.Point;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import tempEngine.PlayerHandler;

/**
 * abstract class design to be a part of a specific version of the well known 
 * Observer Design Pattern.
 * CoordNotifier is our version of Subject
 * 
 * @author HINB's project
 *
 */
public abstract class CoordNotifier {
	/* *********************************************************
	 * Attributes
	 * ********************************************************* */
	private List<PlayerHandler> _listeners;
	
	/* *********************************************************
	 * Constructor(s)
	 * ********************************************************* */
	public CoordNotifier(){
		_listeners = new LinkedList<PlayerHandler>();
	}
	
	/* *********************************************************
	 * Instance Methods
	 * ********************************************************* */
	/**
	 * see Observer Design Pattern
	 * @param game
	 * 
	 * NOTE : in all most of HINB's versions, there will be only one 
	 * attached GameHandler, but using a linked list don't cost that 
	 * much in space or time and is more flexible and could be use to 
	 * attach "debug" handler to print what happend without changing 
	 * code 
	 */
	public void attach(PlayerHandler game){
		_listeners.add(game);
	}
	/**
	 * see Observer Design Pattern
	 * @param game
	 */
	public void detach(PlayerHandler game){
		_listeners.remove(game);
	}
	
	/**
	 * see Observer Design Pattern.
	 * we add a param here to directly send the new state without ask 
	 * the real subject to give it.
	 * @param p
	 * 
	 * NOTE see if it's useful to send this param (i.e. : see if the
	 * implementation of GameHandler already have a reference on the 
	 * real Subject.
	 */
	public void notify(Point p){
		Iterator<PlayerHandler> ite = _listeners.iterator();
		while (ite.hasNext()) {
			PlayerHandler game = ite.next();
			game.update(p);
		}
	}
}
